<?php


namespace backend\controllers;


use common\cache\DictCache;
use common\helpers\commonApi;
use common\models\Campus;
use common\models\Coupon;
use Yii;

class CouponController extends BaseController
{
    public function init()
    {
        parent::init();
        $this->model = Coupon::className();
    }

    public function actionIndex()
    {
        if (Yii::$app->request->isPost){
            $data = parent::actionIndex();
            $coupon_type = DictCache::get('coupon_type');
            $campus_ids = array_column($data['data'],'campus_id');
            $campus = (new Campus)->getCampusName($campus_ids);
            foreach ($data['data'] as &$datum){
                $datum['condition'] = '';
                if ($datum['type']==1){
                    $datum['condition'] = '充值'.$datum['recharge_amount'].'元';
                }
                if ($datum['type']==2){
                    $datum['condition'] = '拉新'.$datum['recharge_amount'].'人';
                }
                $datum['type'] = $coupon_type[$datum['type']]??'';
                $datum['campus'] = $campus[$datum['campus_id']]??'全校通用';
            }
            return $data;
        }
        return parent::actionIndex();
    }

    public function actionAdd()
    {
        if (Yii::$app->request->isPost){
            $post = Yii::$app->request->post();
            $model = new Coupon();
            $result = $model->verific($post);
            if (!$result['success']){
                return $result;
            }
            $model->load($result['data'],'');
            if ($model->validate()&&$model->save()){
                return commonApi::message('添加成功');
            }
            $msg = commonApi::getModelError($model);
            return commonApi::message($msg,false);
        }
        $classify = Campus::getCampusAll(['delete'=>0],['id','name']);
        $coupon_type = DictCache::get('coupon_type');
        return $this->render('',['coupon_type'=>$coupon_type,'classify'=>$classify]);
    }

    public function actionEdit()
    {
        if (Yii::$app->request->isPost){
            $post = Yii::$app->request->post();
            $id = $post['id']??'';
            $model = Coupon::findOne($id);
            if (!$model){
                return commonApi::message('参数错误',false);
            }
            $result = $model->verific($post);
            if (!$result['success']){
                return $result;
            }
            $model->load($result['data'],'');
            if ($model->validate()&&$model->save()){
                return commonApi::message('修改成功');
            }
            $msg = commonApi::getModelError($model);
            return commonApi::message($msg,false);
        }
        $id = Yii::$app->request->get('id','');
        $model = Coupon::findOne($id);
        if (!$model){
            return $this->bError('参数错误');
        }
        $info = $model->toArray();
        $classify = Campus::getCampusAll(['delete'=>0],['id','name']);
        $coupon_type = DictCache::get('coupon_type');
        return $this->render('',['coupon_type'=>$coupon_type,'classify'=>$classify,'info'=>$info]);
    }
}